package com.lanjiu.lib.imclient.ims;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.GroupDto;
import com.lanjiu.lib.imclient.dto.GroupMemberDto;
import com.lanjiu.lib.imclient.dto.chat.ChatMessageDto;
import com.lanjiu.lib.imclient.dto.chat.TextDto;
import com.lanjiu.lib.imclient.dto.extra.ChatGroupMsgDto;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.bean.MsgType;
import com.ljkj.cordial.chat.bean.TextCharSequenceTran;
import com.ljkj.cordial.chat.dao.ChatTableBean;
import com.ljkj.cordial.chat.dao.UserTableBean;
import com.ljkj.cordial.chat.ui.ChatUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/12/28 10:33
 */
public class IMSRequester {
    IMSRequester() {
    }

    public static void requestGroupMemberList(String groupId) {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.GROUP_INFO_REGISTERED : EventConstants.Action.GROUP_INFO_TOURISTS;
        MessageEvent senderEvent = MessageEvent.createSenderEvent(action);
        senderEvent.setData(groupId + ";" + IMSContacts.Contact.NEW_GROUP);
        IMSClientHelper.getInstance().sendRequest(senderEvent);
    }

    public static void requestGroupMemberListDontSayHi(String groupId) {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.GROUP_INFO_REGISTERED : EventConstants.Action.GROUP_INFO_TOURISTS;
        MessageEvent senderEvent = MessageEvent.createSenderEvent(action);
        senderEvent.setData(groupId + ";" + IMSContacts.Contact.OLD_GROUP);
        IMSClientHelper.getInstance().sendRequest(senderEvent);
    }

    public static void sendGroupHi(String groupId, String groupName) {
        ChatTableBean msg = new ChatTableBean();
        UserTableBean u = UserTableBean.getCurrentUser();
        if (u != null) {
            msg.setChatAvatarUrl(u.getAvatar());
            msg.setTargetName(u.getNickname());
            msg.setTargetId(TextUtils.isEmpty(u.getUserId()) ? u.getTemporaryId() : u.getUserId());
            msg.setTargetType(u.getUserType());
        }
        msg.setChatName(groupId);
        msg.setGroupId(groupId);
        msg.setGroupType(IMSUtils.userTypeAnalysis(groupId));
        msg.setFromType(Canstants.GROUP_CHAT);
        msg.setChatType(MsgType.Text);
        msg.setTextMsg("大家好！我是" + UserPreferences.getNickname());
//        TextCharSequenceTran textCharSequenceTran = new TextCharSequenceTran();
//        textCharSequenceTran.setTextTranSpannable(msg.getTextMsg());
//        msg.setCharSequenceText(textCharSequenceTran);
        msg.setMsgTime(System.currentTimeMillis());
        msg.setHistoryTime(System.currentTimeMillis());
        msg.setSendState(ChatTableBean.SEND_ING);
        msg.setChatUnRead(ChatTableBean.READED);
        msg.setStartReadFinish(false);
        ChatTableBean.insertWithNewMsgID(msg);
        MessageEvent event = MessageEvent.cerateUiRefresh(EventConstants.Action.GROUP_CHAT_ADD);
        event.setData(new Gson().toJson(msg));
        EventBus.getDefault().post(event);

        ChatGroupMsgDto chatGroupMsgDto = new ChatGroupMsgDto();
        GroupMemberDto groupMemberDto = new GroupMemberDto(UserPreferences.getUserType());
        groupMemberDto.setGroupMemberUserId(UserPreferences.getUserId());
        groupMemberDto.setGroupNemberName(UserPreferences.getNickname());
        groupMemberDto.setGroupMemberAvatar(UserPreferences.getAvatar());
        GroupDto groupDto = new GroupDto(UserPreferences.getUserType());
        groupDto.setGroupId(groupId);
        groupDto.setGroupName(groupName);
        groupDto.setGroupType(IMSUtils.userTypeAnalysis(groupId));
        groupDto.setSnapchatStatus(false);
        groupMemberDto.setGroup(groupDto);
        chatGroupMsgDto.setGroupMemberDto(groupMemberDto);
        ChatMessageDto chatMessageDto = new ChatMessageDto();
        String msgId = TextUtils.isEmpty(msg.getMsgId()) ? ChatUtils.createMessageId(msg.getId(), UserPreferences.getUserId(), groupMemberDto.getGroup().getGroupId(), msg.getMsgTime()) : msg.getMsgId();
        chatMessageDto.setMsgId(msgId);
        TextDto textDto = new TextDto();
        textDto.setContent(msg.getTextMsg());
        textDto.setLength(String.valueOf(msg.getTextMsg().length()));
        chatMessageDto.setText(textDto);
        chatMessageDto.setChatType(IMSContacts.MsgContentType.TEXT);
        chatGroupMsgDto.setChatMessageDto(chatMessageDto);

        int action = UserPreferences.isRegistered() ? EventConstants.Action.GROUP_CHAT_REGISTERED : EventConstants.Action.GROUP_CHAT_TOURISTS;
        MessageEvent senderEvent = MessageEvent.createSenderEvent(action);
        senderEvent.setData(chatGroupMsgDto.toJson());
        IMSClientHelper.getInstance().sendMessage(senderEvent);
    }
}
